package com.util.security;

import com.util.security.DSA.DSACoder;
import com.util.security.Display.Crypt;
import com.util.security.Display.Decrypt;
import com.util.security.mtoc.MyInput;
import com.util.security.mtoc.Virginia;

import java.util.Map;
import java.util.Scanner;

/**
 * @ClassName: MainExcute
 * @Author: commander
 * @Description: 方法入口
 * @Version: 1.0
 * @Date: 2016/12/18 16:22
 */
public class MainExcute {
	public static void main(String[] args) {
		// write your code here
		System.out.println("1、MD5加密；");
		System.out.println("2、DES加密；");
		System.out.println("3、凯撒算法（古典算法之一）：");
		System.out.println("4、置换加密算法；");
		System.out.println("5、RSA加密算法；");
		System.out.println("6、维吉尼亚加密/解密算法；");
		System.out.println("7、SHA算法；");
		System.out.println("8、DSA签名算法；");
		System.out.println("0、退出程序。");
		System.out.print("请输入数字：");
		Scanner scanner = new Scanner(System.in);
		String choose = scanner.next();
		while (!"0".equals(choose)) {
			switch (choose) {
				case "1":
					System.out.print("请输入要加密的内容：");
					Scanner MD5Input = new Scanner(System.in);
					String md5 = MD5Input.next();
					new MD5().toMD5(md5);//加密LXD
					break;
				case "2":
					System.out.print("请输入要加密的内容：");
					Scanner dataScanner = new Scanner(System.in);
					String data = dataScanner.next();
					System.out.print("请输入key（长度要是8的倍数）：");
					Scanner keyScanner = new Scanner(System.in);
					String key = keyScanner.next();
					byte[] result = DES.encrypt(data.getBytes(), key);
					System.out.println("加密后：" + new String(result));
					//直接将如上内容解密
					try {
						byte[] decryResult = DES.decrypt(result, key);
						System.out.println("解密后：" + new String(decryResult));
					} catch (Exception e1) {
						e1.printStackTrace();
					}
					break;
				case "3":
					Caesar m = new Caesar();
					m.setKey();
					m.getPlaintext();
					m.encryption();
					m.deciphering();
					m.display();
					break;
				case "4":
					System.out.print("请输入明文：");
					String message = new Scanner(System.in).next();
					System.out.print("请输入密钥：");
					String dataKey = new Scanner(System.in).next();
					Crypt crypt = new Crypt(message, dataKey);
					System.out.println("加密：" + crypt.getcrypt());
					Decrypt decrypt = new Decrypt(crypt.getcrypt(), dataKey);
					System.out.println("解密：" + decrypt.gettext());
					break;
				case "5":
					RSAEncrypt rsaEncrypt = new RSAEncrypt();
					System.out.print("请输入要加密的字符串：");
					String source = new Scanner(System.in).next();
					try {
						String cryptograph = rsaEncrypt.encrypt(source);//生成的密文
						System.out.println("生成的密文：" + cryptograph);

						String target = rsaEncrypt.decrypt(cryptograph);//解密密文
						System.out.println("解密后：" + target);
					} catch (Exception e) {
						System.out.println(e.toString());
						e.printStackTrace();
					}
					break;
				case "6":
					System.out.print("请输入明文：");
					String msg = MyInput.readString();
					System.out.print("请输入key：");
					String k = MyInput.readString();
					new Virginia().vga(k, msg);
					break;
				case "7":
					try {
						System.out.print("请输入要加密的内容：");
						String str = new Scanner(System.in).next();
						System.out.println("SHA后：" + new SHAUtil().shaEncode(str));
					} catch (Exception e) {
						System.out.println(e.toString());
						e.printStackTrace();
					}
					break;
				case "8":
					try {
						DSACoder dsaCoder = new DSACoder();
						System.out.print("请输入加密内容：");
						String inputStr = new Scanner(System.in).next();
						byte[] dataByte = inputStr.getBytes();

						// 构建密钥
						Map<String, Object> keyMap = dsaCoder.initKey();

						// 获得密钥
						String publicKey = DSACoder.getPublicKey(keyMap);
						String privateKey = DSACoder.getPrivateKey(keyMap);

						System.out.println("公钥:" + publicKey);
						System.out.println("私钥:" + privateKey);

						// 产生签名
						String sign = DSACoder.sign(dataByte, privateKey);
						System.out.println("签名:" + sign);

						// 验证签名
						boolean status = DSACoder.verify(dataByte, publicKey, sign);
						System.out.println("状态:" + status);
					} catch (Exception e) {
						System.out.println(e.toString());
						e.printStackTrace();
					}
					break;
			}
			System.out.println("1、MD5加密；");
			System.out.println("2、DES加密；");
			System.out.println("3、凯撒算法（古典算法之一）：");
			System.out.println("4、置换加密算法；");
			System.out.println("5、RSA加密算法；");
			System.out.println("6、维吉尼亚加密/解密算法；");
			System.out.println("7、SHA算法；");
			System.out.println("8、DSA签名算法；");
			System.out.println("0、退出程序。");
			System.out.print("请输入数字：");
			Scanner sca = new Scanner(System.in);
			choose = sca.next();
		}
		System.out.println("**********感谢使用**********");
	}
}
